package com.osdp.uc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.osdp.common.constant.CommonConstant;
import com.osdp.common.util.BeansUtil;
import com.osdp.common.util.ConvertUtil;
import com.osdp.uc.entity.UcTitle;
import com.osdp.uc.mapper.UcTitleMapper;
import com.osdp.uc.service.IUcTitleService;
import com.osdp.uc.util.Fields;
import com.osdp.uc.vo.UcTitleVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UcTitleServiceImpl extends ServiceImpl<UcTitleMapper, UcTitle> implements IUcTitleService {

    @Override
    public IPage<UcTitleVo> pageList(UcTitleVo titleVo, Page<UcTitle> page) {
        return super.page(page, new QueryWrapper<UcTitle>()
                .like(StringUtils.isNotBlank(titleVo.getTitleCode()), Fields.UcTitleQ.titleCode,titleVo.getTitleCode())
                .like(StringUtils.isNotBlank(titleVo.getTitleName()), Fields.UcTitleQ.titleName,titleVo.getTitleName())
                .eq(StringUtils.isNotBlank(titleVo.getTitleLevel()),Fields.UcTitleQ.titleLevel,titleVo.getTitleLevel())
                .eq(StringUtils.isNotBlank(titleVo.getValidation()),Fields.UcTitleQ.validation,titleVo.getValidation()))
                .convert(track -> BeansUtil.copy(track, new UcTitleVo()));
    }

    @Override
    public boolean insertTitile(UcTitleVo titleVo) {
        UcTitle entity = ConvertUtil.sourceToTarget(titleVo, UcTitle.class);
        return super.save(entity);
    }

    @Override
    public boolean updateTitile(UcTitleVo titleVo) {
        UcTitle entity = ConvertUtil.sourceToTarget(titleVo, UcTitle.class);
        return super.updateById(entity);
    }

    @Override
    public UcTitleVo getByTitileId(String id) {
        UcTitle entity = super.getById(id);
        return ConvertUtil.sourceToTarget(entity, UcTitleVo.class);
    }

    @Override
    public boolean deleteTitile(String id) {
        return super.removeById(id);
    }

    @Override
    public List<UcTitleVo> queryAllTitle(String titleName,String titleCode){
        List<UcTitle> list = this.list(new QueryWrapper<UcTitle>()
                .like(StringUtils.isNotBlank(titleName), Fields.UcTitleQ.titleName,titleName)
                .like(StringUtils.isNotBlank(titleCode), Fields.UcTitleQ.titleCode,titleCode)
                .eq(Fields.UcTitleQ.validation, CommonConstant.FLAG_ONE_1));
        return ConvertUtil.sourceToTarget(list, UcTitleVo.class);


    }
}
